<?php

namespace App\Console\Commands;

use App\Models\FlowCollection;
use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\ParkInfo;
use App\Models\ParkRecord;
use App\Models\Report;
use Illuminate\Support\Facades\Log;

class FlowNumberDay extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'flow-num:count_day';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '收入统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 支付记录信息定时汇总 计划任务
     * @author wangxb
     * @email  wangxiaobo@parkingwang.com
     *
     * @return mixed
     */
    public function handle()
    {
        ini_set('memory_limit', '128M');
        $start = "2018-06-01 05:00:00";
        $end = "2018-06-15 05:00:00";
        $date = strtotime($start);
        $dateEnd = strtotime($end);

        FlowCollection::where('hour','>=',$start)->where('hour','<=',$end)->delete();

        while ($date < $dateEnd) {
            $this->index($date);
            $date += 3600;
        }

        $this->info('最终完成');

    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function index($data)
    {
        Log::info('[定时任务]<支付记录汇总>：开始');
        $this->info(date('Y-m-d H:i:s',$data) . ' [定时任务]<支付记录汇总>: 执行开始');
//         定时任何 汇总时间 区间(前闭后开)
        $times = [
            strtotime(date('Y-m-d H:00:00', $data)) - 3600,
            strtotime(date('Y-m-d H:00:00',$data)),
        ];

        $start = $times[0];
        $end = $times[1];
//        $start = Carbon::yesterday()->timestamp;
//        $end = Carbon::today()->timestamp;

        $dateTime = Carbon::createFromTimestamp($start);
        $hour = Carbon::create($dateTime->year, $dateTime->month, $dateTime->day, $dateTime->hour);

        $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
            ->select(['park_code', 'parking_name'])
            ->orderBy('sort_by', 'desc')
            ->get();

        $data = $parks->map(function ($item, $key) use ($hour, $start, $end) {
            $flowCollection = FlowCollection::firstOrNew(
                [
                    'park_code' => $park_code,
                    'hour' => $hour
                ],
                [
                    'leave_flow' => 0,
                    'short_time_free' => 0,
                    'self_free' => 0,
                    'worker_car' => 0,
                    'inner_car' => 0,
                    'free_car' => 0,
                    'blank_car' => 0,
                    'other_free' => 0,
                    'free_car_total' => 0,
                    'big_car' => 0,
                    'taxi' => 0,
                    'small_car' => 0,
                    'other_charge_car' => 0,
                    'etc_charge_car' => 0,
                    'etc_free_car' => 0,
                    'etc_small_car' => 0,
                    'etc_big_car' => 0,
                ]
            );
            //查询前一天数据
            $table = 't_park_record_' . $item->park_code;
            $parkRecord = new ParkRecord();
            $parkRecord->setTable($table);
            $res = $parkRecord
                ->where('out_time', '>=', $start)
                ->where('out_time', '<', $end)
                ->select(['parking_charge', 'out_time', 'vpl_type', 'charge_items'])->get();

            foreach ($res as $val) {
                $flowCollection->leave_flow += 1;

                //免费车
                $vplType = $event->data->vpl_type;
                if ($event->data->parking_charge < 1) {
                    //收费项
                    $items = $event->data->charge_items;
                    if ($event->data->etc_flag) {
                        $flowCollection->etc_free_car += 1;
                    }
                    if (count($items) > 0) {
                        foreach ($items as $v) {
                            //手动抬干
                            if ($v->charge_type == 8) {
                                $vplType = -1;
                            }
                            //短时苗木费
                            if ($v->charge_type == 0) {
                                $vplType = -2;
                            }
                        }
                    }
                    switch ($vplType) {
                        case -1://手动抬干
                            $flowCollection->self_free += 1;
                            break;
                        case -2://短时免费
                            $flowCollection->short_time_free += 1;
                            break;
                        case 8:  //优惠车
                            $flowCollection->worker_car += 1;
                            break;
                        case 10:// 免费车
                            $flowCollection->inner_car += 1;
                            break;
                        case 9:// 员工车
                            $flowCollection->free_car += 1;
                            break;
                        case 1:// 白名单
                            $flowCollection->blank_car += 1;
                            break;
                        default:
                            $flowCollection->other_free += 1;

                    }

                    $flowCollection->free_car_total += 1;

                } else {
                    if ($event->data->etc_flag) {
                        $flowCollection->etc_charge_car += 1;
                    }
                    //收费车
                    $vpl_color = $event->data->vpl_color;
                    switch ($vpl_color) {
                        case 0:
                            $flowCollection->small_car += 1;
                            $flowCollection->etc_small_car += 1;
                            break;
                        case 1:
                            $flowCollection->big_car += 1;
                            $flowCollection->etc_big_car += 1;
                            break;
                        default:
                            $flowCollection->other_charge_car += 1;
                    }
                    if ($event->data->park_code == '7100000319') {
                        $flowCollection->taxi += 1;
                    }

                    $flowCollection->total_charge_car += 1;
                }
            }
            $flowCollection->save();
        });


        $this->info('收入统计成功');
    }

    /**
     * 计算时间范围
     *
     * @param $value
     * @param $key
     * @return string
     */
    private function initFiled($value, $key)
    {
        $time = ceil($value->parking_duration / 60);
        if ($time > 0 && $time <= 15) return "{$key}_0_15";
        if ($time > 15 && $time <= 30) return "{$key}_15_30";

        for ($i = 30; $i < 1440; $i += 30) {
            if ($time > $i && $time <= $i + 30) return "{$key}_" . $i . "_" . ($i + 30);
        }

        return $key . "_greater_1440";
    }
}
